
var detailData;
var siteBookTable;
function getrobotManagerList() {
    if (typeof siteBookTable !== 'undefined') {
        siteBookTable.fnPageChange('first');
        return;
    }
    siteBookTable = $("#robotManagerList").DataTable({
        "sAjaxSource": _path + "/robotManager/queryByPage",     //服务端
        "fnServerParams": function (aoData) {               //ajax请求参数
            aoData.push(
                { "name": "name", "value": $("#name").val()},
                { "name": "fromDate", "value": $("#fromDate").val() },
                { "name": "toDate", "value": $("#toDate").val() }
            );
        },
        "fnServerData": function(sUrl, aoData, fnCallback) {
            var data = new Array();
            for(var i in aoData)
                data.push(aoData[i].name + '=' + aoData[i].value);
            $.ajax( {
                "type": "POST",
                "url": sUrl,
                "dataType": "json",
                "data": data.join('&'),
                "success": function(resp) {
                    detailData = resp.dataList;
                    fnCallback(resp);
                }
            });
        },
        "sServerMethod": "POST",    //请求方式
        "sAjaxDataProp": "dataList",//后台返回数据名
        "aoColumns": [
            { "sTitle": "ID", "sWidth": "6%", "mData": "id" },
            { "sTitle": "名称", "sWidth": "6%", "mData": "name" },
            { "sTitle": "最后采集时间", "sWidth": "6%", "mData": "lasttime" },
            { "sTitle": "内容操作", "sWidth": "10%",
                "mData": function (data) {
                	 var html = new Array();
                     html.push('<a href="javascript:void(0);" onclick="robotURL(\'' + data.id + '\');">[采集网址]</a>');
                     html.push('&nbsp;<a href="javascript:void(0);" onclick="robotContent(\'' + data.id + '\');">[采集内容]</a>');
                     html.push('&nbsp;<a href="javascript:void(0);" onclick="publishContent(\'' + data.id + '\');">[内容发布]</a>');
                     return html.join('');
                }
            },
            { "sTitle": "操作", "sWidth": "10%",
                "mData": function (data) {
                	var html = new Array();
                    html.push('<a href="javascript:void(0);" onclick="update(\'' + data.id + '\');">[修改]</a>');
                    return html.join('');
                }
            }
        ]
    });
}

function save() {
	App.ajax(_path + "/robotManager/add", $("#formDetail").serialize(), saveResult);
}

function saveResult(data) {
    if (data == '200') {
        App.bubble(200, "保存成功！");
        window.setTimeout(function(){
        	window.location.href = _path + "/robotManager/list";
        }, 500);
    } else if (data == '500') {
        App.bubble(500, "保存失败！");
    }
}

function robotURL(id) {
	App.bubble(100, "正在采集，请稍后。。。");
	App.ajax(_path + "/robotManager/robotURL", "id="+id, function (data) {
		switch (data) {
        case "200" :
            App.bubble(data, "采集成功！");
            break;
        case "500" :
            App.bubble(data, "采集失败！");
            break;
        case "404" :
            App.bubble(100, "没有采集到数据！");
            break;
        default :
            App.bubble(500, "操作失败！");
            break;
        }
    });
}

function robotContent(id) {
	App.bubble(100, "正在采集，请稍后。。。");
	App.ajax(_path + "/robotManager/robotContent", "id="+id, function (data) {
		switch (data) {
        case "200" :
            App.bubble(data, "采集成功！");
            break;
        case "500" :
            App.bubble(data, "采集失败！");
            break;
        case "404" :
            App.bubble(100, "没有需要采集的数据！");
            break;
        default :
            App.bubble(500, "操作失败！");
            break;
        }
    });
}

function publishContent(id) {
	App.bubble(100, "正在发布，请稍后。。。");
	App.ajax(_path + "/robotManager/publishContent", "id="+id, function (data) {
		switch (data) {
        case "200" :
            App.bubble(data, "发布成功！");
            break;
        case "500" :
            App.bubble(data, "发布失败！");
            break;
        case "404" :
            App.bubble(100, "没有需要发布的数据！");
            break;
        default :
            App.bubble(500, "操作失败！");
            break;
        }
    });
}

function update(id) {
	window.location.href = _path + "/robotManager/update/" + id;
}

function changeSecondType(pid, columnId) {
    $.ajax({
        url: _path + '/conf/queryConfTByPid?pid='+pid,
        type: 'GET',
        success: function(resp) {
            $("#columnId").html('');
            new LinkageSel({
                dataReader: {id: 'id', name: 'value0'},
                data: resp.dataList,
                selStyle: 'margin-left: 3px;',
                select:  '#columnId',
                defVal: [columnId]
            });
        }
    });
}